package intelligentcity.system;

import intelligentcity.conf.LightsConf;

public class LightsLearnedByRatio extends IntelligentLights {

    public LightsLearnedByRatio() {
        super();
    }

    public LightsLearnedByRatio(int numberOfState) {
        super(numberOfState);
    }

    @Override
    protected void think() {
        
        int amountNS = diary.getEntryNorth() + diary.getEntrySouth();
        int amountWE = diary.getEntryWest() + diary.getEntryEast();    
        if(amountNS == 0 && amountWE == 0) return;
        
        if(amountNS == 0) {
            setTimeNS(LightsConf.minTimeOfPassableState);
            setTimeWE(LightsConf.maxTimeOfPassableState);
            return;
        }
        else if(amountWE == 0) {
            setTimeNS(LightsConf.minTimeOfPassableState);
            setTimeWE(LightsConf.maxTimeOfPassableState);
            return;
        }
               
        int ratioCars = amountNS / amountWE;
        int ratioLights = getTimeNS() / getTimeWE();

        if (ratioCars > ratioLights) {
            setTimeNS(getTimeNS() + LightsConf.timeJump);
            setTimeWE(getTimeWE() - LightsConf.timeJump);
        } else {
            setTimeNS(getTimeNS() - LightsConf.timeJump);
            setTimeWE(getTimeWE() + LightsConf.timeJump);
        }
    }
}
